<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 400px;
            margin: 100px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .login-form {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #555;
        }
        input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0069d9;
        }
        .error {
            color: red;
            text-align: center;
            margin-top: 15px;
        }
        .success {
            color: green;
            text-align: center;
            margin-top: 15px;
        }
    </style>
</head>
<body>
    <div class="login-form">
        <h2>用户登录</h2>
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" required>
            </div>
            <button type="submit">登录</button>
        </form>
        <div id="message"></div>
    </div>

    <script src="/js/http-client.js"></script>
    <script>
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const messageDiv = document.getElementById('message');
            
            try {
                const result = await httpClient.post('/auth/login', { username, password });
                // 处理标准响应格式
                if (result.code === 0) {
                    // 保存 token 到 localStorage
                    if (result.data && result.data.access_token) {
                        localStorage.setItem('jwt_token', result.data.access_token);
                    }
                    
                    messageDiv.innerHTML = '<div class="success">登录成功，正在跳转...</div>';
                    setTimeout(() => {
                        window.location.href = '/admin';
                    }, 500);
                } else {
                    const message = result.message || '登录失败';
                    messageDiv.innerHTML = '<div class="error">' + message + '</div>';
                }
            } catch (error) {
                messageDiv.innerHTML = '<div class="error">登录请求失败: ' + error.message + '</div>';
            }
        });
    </script>
</body>
</html>